// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using SoapTrace.Core.Formatters;
using SoapTrace.Tests.Utils;
using SoapTrace.Tests.Config;
using System;
using System.Configuration;
using System.IO;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Test fixture to test the WebServicesDescriptionLanguageFile object's 
	/// handling of an RPC wsdl file.
	/// </summary>
	[ TestFixture ]
	public class RpcWsdlFileTestFixture
	{
		/// <summary>
		/// The wsdl file object under test.
		/// </summary>
		private WebServicesDescriptionLanguageFile m_WsdlFile;

		/// <summary>
		/// The web service associated with the wsdl file.
		/// </summary>
		private WebService m_WebService;

		/// <summary>
		/// The web service operations associated with the wsdl file.
		/// </summary>
		private WebServiceOperationCollection m_WebServiceOperations;

		/// <summary>
		/// Expected web service details.
		/// </summary>
		private WebService m_ExpectedWebService;

		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			// Set up the expected web service.

			m_ExpectedWebService = new WebService( );
			m_ExpectedWebService.Name = "TestEJB";
			m_ExpectedWebService.Uri = "/TestEJB/TestEJBuri";

			AddExpectedWebServiceOperations( );

			m_WsdlFile = new WebServicesDescriptionLanguageFile( );

			string WsdlXml = GetWsdl( );
			
			StringReader Reader = new StringReader( WsdlXml );
			
			m_WsdlFile.Load( Reader );

			m_WebService = m_WsdlFile.WebService;

			m_WebServiceOperations = m_WebService.Operations;
		}

		/// <summary>
		/// Check that the wsdl file reads the correct web service name
		/// </summary>
		[ Test ]
		public void WebServiceName( )
		{
			Assert.AreEqual( m_ExpectedWebService.Name, m_WebService.Name,
				"Web service name is incorrect." );
		}

		/// <summary>
		/// Check the uri extracted from the wsdl file is correct.
		/// </summary>
		[ Test ]
		public void WebServiceUri( )
		{
			Assert.AreEqual( m_ExpectedWebService.Uri, m_WebService.Uri,
				"Web service uri is incorrect." );
		}

		/// <summary>
		/// Checks that the correct number of web services were extracted.
		/// </summary>
		[ Test ]
		public void WebServiceCount( )
		{
			Assert.AreEqual( m_ExpectedWebService.Operations.Count, 
				m_WebServiceOperations.Count, 
				"Web service count is incorrect." );
		}

		/// <summary>
		/// Tests the first web service operation
		/// </summary>
		[ Test ]
		public void WebServiceOperations( )
		{
			for( int i = 0; i < m_WebServiceOperations.Count; ++i )
			{
				WebServiceOperation Operation = m_WebServiceOperations[ i ];

				WebServiceOperation ExpectedOperation = m_ExpectedWebService.Operations[ i ];

				Assert.AreEqual( ExpectedOperation.Name, Operation.Name,
					"Expected operation name is incorrect." );

				Assert.AreEqual( ExpectedOperation.Parameters.Count, 
					Operation.Parameters.Count,
					"Expected number of parameters is wrong." );

				WebServiceTestUtility.CompareParameters( ExpectedOperation, Operation );

				WebServiceTestUtility.CompareReturnValue( ExpectedOperation, Operation );
			}
		}

		/// <summary>
		/// Adds expected web service operations to the web service.
		/// </summary>
		private void AddExpectedWebServiceOperations( )
		{
			// Add first operation.

			WebServiceOperation Operation = new WebServiceOperation( );
			Operation.Name = "method1";

			// Add parameters.

			AddExpectedParameters( Operation );

			// Add return values.

			AddExpectedReturnValue( Operation );

			m_ExpectedWebService.Operations.Add( Operation );


			// Add second operation.

			Operation = new WebServiceOperation( );
			Operation.Name = "method2";

			// Add parameters.

			AddExpectedParameters( Operation );

			// Add return values.

			AddExpectedReturnValue( Operation );

			m_ExpectedWebService.Operations.Add( Operation );

		}

		/// <summary>
		/// Adds expected parameters to the web service operation.
		/// </summary>
		/// <param name="operation">The web service operation.</param>
		private void AddExpectedParameters( WebServiceOperation operation )
		{
			WebServiceParameter Parameter = new WebServiceParameter( );
			Parameter.Name = "string";
			Parameter.Display = true;
			Parameter.ParameterType = "string";
			operation.Parameters.Add( Parameter );			

			Parameter = new WebServiceParameter( );
			operation.Parameters.Add( Parameter );
			Parameter.Name = "string0";
			Parameter.Display = true;
			Parameter.ParameterType = "string";
		}

		/// <summary>
		/// Adds expected return value to the web service operation.
		/// </summary>
		/// <param name="operation">The web service operation.</param>
		private void AddExpectedReturnValue( WebServiceOperation operation )
		{
			WebServiceReturnValue ReturnValue = new WebServiceReturnValue( );
			ReturnValue.Name = "result";
			ReturnValue.Display = true;
			ReturnValue.ParameterType = "string.array";
			operation.ReturnValue = ReturnValue ;
		}
		
		/// <summary>
		/// Gets the wsdl used in this text fixture.
		/// </summary>
		/// <returns>Wsdl xml.</returns>
		private string GetWsdl( )
		{
			return "<definitions  xmlns:tns=\"java:com.test.ws.TestEJB\"\r\n" +
				"  xmlns:wsr=\"http://www.openuri.org/2002/10/soap/reliability/\"\r\n" +
				"  xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"\r\n" +
				"  xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\"\r\n" +
				"  xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\"\r\n" +
				"  xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"  xmlns:soap12enc=\"http://www.w3.org/2003/05/soap-encoding\"\r\n" +
				"  xmlns:conv=\"http://www.openuri.org/2002/04/wsdl/conversation/\"\r\n" +
				"  xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"\r\n" +
				"  xmlns:s=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"  xmlns=\"http://schemas.xmlsoap.org/wsdl/\"\r\n" +
				"  targetNamespace=\"java:com.test.ws.TestEJB\">\r\n" +
				" <types>\r\n" +
				"  <xsd:schema    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"    xmlns:stns=\"java:language_builtins.lang\"\r\n" +
				"    elementFormDefault=\"qualified\"\r\n" +
				"    attributeFormDefault=\"qualified\"\r\n" +
				"    targetNamespace=\"java:language_builtins.lang\">\r\n" +
				"   <xsd:import     namespace=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" +
				"   </xsd:import>\r\n" +
				"   <xsd:complexType     name=\"ArrayOfString\">\r\n" +
				"    <xsd:complexContent>\r\n" +
				"     <xsd:restriction       xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"       base=\"soapenc:Array\">\r\n" +
				"      <xsd:attribute        xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"\r\n" +
				"        ref=\"soapenc:arrayType\"\r\n" +
				"        wsdl:arrayType=\"xsd:string[]\">\r\n" +
				"      </xsd:attribute>\r\n" +
				"     </xsd:restriction>\r\n" +
				"    </xsd:complexContent>\r\n" +
				"   </xsd:complexType>\r\n" +
				"  </xsd:schema>\r\n" +
				" </types>\r\n" +
				" <message   name=\"method1\">\r\n" +
				"  <part    xmlns:partns=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"    type=\"partns:string\"\r\n" +
				"    name=\"string\">\r\n" +
				"  </part>\r\n" +
				"  <part    xmlns:partns=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"    type=\"partns:string\"\r\n" +
				"    name=\"string0\">\r\n" +
				"  </part>\r\n" +
				" </message>\r\n" +
				" <message   name=\"method1Response\">\r\n" +
				"  <part    xmlns:partns=\"java:language_builtins.lang\"\r\n" +
				"    type=\"partns:ArrayOfString\"\r\n" +
				"    name=\"result\">\r\n" +
				"  </part>\r\n" +
				" </message>\r\n" +
				" <message   name=\"method2\">\r\n" +
				"  <part    xmlns:partns=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"    type=\"partns:string\"\r\n" +
				"    name=\"string\">\r\n" +
				"  </part>\r\n" +
				"  <part    xmlns:partns=\"http://www.w3.org/2001/XMLSchema\"\r\n" +
				"    type=\"partns:string\"\r\n" +
				"    name=\"string0\">\r\n" +
				"  </part>\r\n" +
				" </message>\r\n" +
				" <message   name=\"method2Response\">\r\n" +
				"  <part    xmlns:partns=\"java:language_builtins.lang\"\r\n" +
				"    type=\"partns:ArrayOfString\"\r\n" +
				"    name=\"result\">\r\n" +
				"  </part>\r\n" +
				" </message>\r\n" +
				" <portType   name=\"TestEJBPort\">\r\n" +
				"  <operation    name=\"method1\">\r\n" +
				"   <input     message=\"tns:method1\">\r\n" +
				"   </input>\r\n" +
				"   <output     message=\"tns:method1Response\">\r\n" +
				"   </output>\r\n" +
				"  </operation>\r\n" +
				"  <operation    name=\"method2\">\r\n" +
				"   <input     message=\"tns:method2\">\r\n" +
				"   </input>\r\n" +
				"   <output     message=\"tns:method2Response\">\r\n" +
				"   </output>\r\n" +
				"  </operation>\r\n" +
				" </portType>\r\n" +
				" <binding   type=\"tns:TestEJBPort\"\r\n" +
				"   name=\"TestEJBPort\">\r\n" +
				"  <soap:binding    style=\"rpc\"\r\n" +
				"    transport=\"http://schemas.xmlsoap.org/soap/http\">\r\n" +
				"  </soap:binding>\r\n" +
				"  <operation    name=\"method1\">\r\n" +
				"   <soap:operation     style=\"rpc\"\r\n" +
				"     soapAction=\"\">\r\n" +
				"   </soap:operation>\r\n" +
				"   <input>\r\n" +
				"    <soap:body      namespace=\"java:com.test.ws.TestEJB\"\r\n" +
				"      encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"      use=\"encoded\">\r\n" +
				"    </soap:body>\r\n" +
				"   </input>\r\n" +
				"   <output>\r\n" +
				"    <soap:body      namespace=\"java:com.test.ws.TestEJB\"\r\n" +
				"      encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"      use=\"encoded\">\r\n" +
				"    </soap:body>\r\n" +
				"   </output>\r\n" +
				"\r\n" +
				"  </operation>\r\n" +
				"  <operation    name=\"method2\">\r\n" +
				"   <soap:operation     style=\"rpc\"\r\n" +
				"     soapAction=\"\">\r\n" +
				"   </soap:operation>\r\n" +
				"   <input>\r\n" +
				"    <soap:body      namespace=\"java:com.test.ws.TestEJB\"\r\n" +
				"      encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"      use=\"encoded\">\r\n" +
				"    </soap:body>\r\n" +
				"   </input>\r\n" +
				"   <output>\r\n" +
				"\r\n" +
				"    <soap:body      namespace=\"java:com.test.ws.TestEJB\"\r\n" +
				"      encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" +
				"      use=\"encoded\">\r\n" +
				"    </soap:body>\r\n" +
				"   </output>\r\n" +
				"  </operation>\r\n" +
				" </binding>\r\n" +
				" <service   name=\"TestEJB\">\r\n" +
				"  <port    name=\"TestEJBPort\"\r\n" +
				"    binding=\"tns:TestEJBPort\">\r\n" +
				"   <soap:address     location=\"http://myserver:7001/TestEJB/TestEJBuri\">\r\n" +
				"   </soap:address>\r\n" +
				"  </port>\r\n" +
				" </service>\r\n" +
				"</definitions>";
		}
	}
}
